﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace VoltGameEditor
{
	public class TextureLayoutPanel : FlowLayoutPanel
	{
		Collection<Collection<Control>> _pages = new Collection<Collection<Control>>();

		public TextureLayoutPanel()
		{
			DoubleBuffered = true;
		}

		public Collection<Collection<Control>> Pages
		{
			get { return _pages; }
		}

		VScrollBar _scrollBar;

		public VScrollBar ScrollBar
		{
			get { return _scrollBar; }
			set
			{
				if (_scrollBar != null)
					_scrollBar.ValueChanged -= OnScrollbarMoved;

				_scrollBar = value;
				_scrollBar.ValueChanged += OnScrollbarMoved;
			}
		}

		protected void OnScrollbarMoved(object sender, EventArgs e)
		{
			if (OnPageChanged(_scrollBar.Value))
				_curPage = _scrollBar.Value;
		}

		int _curPage = 0;

		public int CurPage
		{
			get { return _curPage; }
			set { if (OnPageChanged(value)) _curPage = value; }
		}

		protected bool OnPageChanged(int NewPage)
		{
			if (NewPage < 0 || NewPage >= _pages.Count)
				return false;

			SuspendLayout();
			Controls.Clear();
			Controls.AddRange(_pages[NewPage].ToArray<Control>());
			ResumeLayout();

			_scrollBar.Value = NewPage;

			return true;
		}

		public void UpdatePage()
		{
			OnPageChanged(_curPage);
		}

		public void SetPages(IEnumerable<Control> ControlsToAdd, int ControlWidth, int ControlHeight)
		{
			int PerRow = Width / ControlWidth;
			int PerColumn = Height / ControlHeight;
			int PerPage = PerRow * PerColumn;

			int CurControl = 0;
			var Enumerator = ControlsToAdd.GetEnumerator();

			for (int Page = 0; CurControl < ControlsToAdd.Count<Control>(); ++Page)
			{
				Collection<Control> pg = new Collection<Control>();

				for (int i = 0; i < PerPage; ++i)
				{
					if (!Enumerator.MoveNext())
						break;

					pg.Add(Enumerator.Current);
					CurControl++;
				}

				if (pg.Count != 0)
					_pages.Add(pg);
			}

			_scrollBar.Maximum = _pages.Count;
			_scrollBar.LargeChange = 5;

			UpdatePage();
		}
	}

	public class TextureLayoutPanel2 : Panel
	{
		Collection<FlowLayoutPanel> _pages = new Collection<FlowLayoutPanel>();

		public TextureLayoutPanel2()
		{
			DoubleBuffered = true;
		}

		public Collection<FlowLayoutPanel> Pages
		{
			get { return _pages; }
		}

		public int Count
		{
			get
			{
				int c = 0;

				for (int i = 0; i < _pages.Count; ++i)
					c += _pages[i].Controls.Count;

				return c;
			}
		}

		VScrollBar _scrollBar;

		public VScrollBar ScrollBar
		{
			get { return _scrollBar; }
			set
			{
				if (_scrollBar != null)
					_scrollBar.ValueChanged -= OnScrollbarMoved;

				_scrollBar = value;
				_scrollBar.ValueChanged += OnScrollbarMoved;
			}
		}

		protected void OnScrollbarMoved(object sender, EventArgs e)
		{
			if (OnPageChanged(_scrollBar.Value))
				_curPage = _scrollBar.Value;
		}

		int _curPage = 0;

		public int CurPage
		{
			get { return _curPage; }
			set { if (OnPageChanged(value)) _curPage = value; }
		}

		protected bool OnPageChanged(int NewPage)
		{
			if (NewPage < 0 || NewPage >= _pages.Count)
				return false;

			SuspendLayout();
			Controls.Clear();
			Controls.Add(_pages[NewPage]);
			ResumeLayout();

			_scrollBar.Value = NewPage;

			return true;
		}

		public void UpdatePage()
		{
			OnPageChanged(_curPage);
		}

		public void SetPages(IEnumerable<Control> ControlsToAdd, int ControlWidth, int ControlHeight)
		{
			int PerRow = Width / ControlWidth;
			int PerColumn = Height / ControlHeight;
			int PerPage = PerRow * PerColumn;

			int CurControl = 0;
			var Enumerator = ControlsToAdd.GetEnumerator();

			for (int Page = 0; CurControl < ControlsToAdd.Count<Control>(); ++Page)
			{
				Collection<Control> pg = new Collection<Control>();

				for (int i = 0; i < PerPage; ++i)
				{
					if (!Enumerator.MoveNext())
						break;

					pg.Add(Enumerator.Current);
					CurControl++;
				}

				if (pg.Count != 0)
				{
					FlowLayoutPanel fp = new FlowLayoutPanel();
					fp.Controls.AddRange(pg.ToArray<Control>());
					fp.Dock = DockStyle.Fill;

					_pages.Add(fp);
				}
			}

			_scrollBar.Maximum = _pages.Count;
			_scrollBar.LargeChange = 5;

			UpdatePage();
		}
	}
}
